﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZJ_DZJEInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);

            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            string strbid = base.Page.Request["BID"];
            string strid = base.Page.Request["ID"];
            if (string.IsNullOrEmpty(strid))
            {
                if (_EluZSDataProvider.GetNewMoneyISFirst(strbid))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displaytr", "<script>displaytr();</script>");
                }
            }
            else
            {
                if (!_EluZSDataProvider.GetNewMoneyNowIFT(strid, strbid))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "displaytr", "<script>displaytr();</script>");
                }
            }
            TextBox txt到账金额 = (TextBox)FindControl("txt到账金额");
            TextBox txt是否首期 = (TextBox)FindControl("txt是否首期");
            MPCalendar txt到账时间 = (MPCalendar)FindControl("txt到账时间");
            TextBox txt备注 = (TextBox)FindControl("txt备注");

            TextBox txtBID = (TextBox)FindControl("txtBID");
            TextBox txtURID = (TextBox)FindControl("txtURID");
            TextBox txtID = (TextBox)FindControl("txtID");

            string strpid = base.Page.Request["PID"];
            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(strbid)) { if (string.IsNullOrEmpty(txtBID.Text.Trim())) { txtBID.Text = strbid; } }
                if (!string.IsNullOrEmpty(strid))
                {
                    if (string.IsNullOrEmpty(txtID.Text.Trim())) { txtID.Text = strid; }
                    EluZSProvider.NEW_MONEY newmoney = _EluZSDataProvider.GetNewmoney(strid);

                    txt到账金额.Text = newmoney.到账金额.ToString();
                    txt是否首期.Text = newmoney.是否首期;
                    txtBID.Text = newmoney.BID;
                    txtURID.Text = newmoney.URID;
                    txtID.Text = newmoney.ID;
                    if (newmoney.到账时间.HasValue) { txt到账时间.Text = newmoney.到账时间.Value.ToShortDateString(); }
                    txt备注.Text = newmoney.备注;
                }
                else
                {
                    txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "");
                    txtURID.Text = userid;
                }
            }

        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            string strbid = base.Page.Request["BID"];
            string strpid = base.Page.Request["PID"];
            TextBox txt到账金额 = (TextBox)FindControl("txt到账金额");
            TextBox txt是否首期 = (TextBox)FindControl("txt是否首期");
            MPCalendar txt到账时间 = (MPCalendar)FindControl("txt到账时间");
            TextBox txt备注 = (TextBox)FindControl("txt备注");

            TextBox txtBID = (TextBox)FindControl("txtBID");
            TextBox txtURID = (TextBox)FindControl("txtURID");
            TextBox txtID = (TextBox)FindControl("txtID");
            EluZSProvider.NEW_MONEY newmoney = new EluZSProvider.NEW_MONEY();

            newmoney.到账金额 = txt到账金额.Text;
            newmoney.是否首期 = txt是否首期.Text.Trim();
            newmoney.BID = txtBID.Text.Trim();
            newmoney.URID = txtURID.Text.Trim();
            newmoney.ID = txtID.Text.Trim();
            newmoney.到账时间 = (string.IsNullOrEmpty(txt到账时间.Text) ? null : (DateTime?)Convert.ToDateTime(txt到账时间.Text));
            newmoney.备注 = txt备注.Text.Trim();

            bool save = _EluZSDataProvider.SaveNewmoney(strid, newmoney);
            if (save) { base.Page.Response.Write("<script>alert('保存成功！');window.location.href('ZJ_DZJEInfo.aspx?ID=" + txtID.Text + "&PID=" + strpid + "&BID=" + strbid + "');</script>"); }
        }
    }
}


